using System;
using UnityEngine.SceneManagement;

public class SceneMgr : ManagerBase
{
    public static SceneMgr Instance;

    private void Awake()
    {
        Instance = this;

        SceneManager.sceneLoaded += SceneManager_sceneLoaded;
        
        Add(SceneEvent.LOAD_SCENE, this);
    }


    public override void Execute(int eventCode, object message)
    {
        switch (eventCode)
        {
            case SceneEvent.LOAD_SCENE:
                LoadSceneMsg msg = message as LoadSceneMsg;
                loadScene(msg);
                break;
            default:
                break;
        }
    }

    /// <summary>
    /// 临时变量, 外界不需要访问
    /// </summary>
    private Action onSceneLoaded = null;


    /// <summary>
    /// 加载场景
    /// </summary>
    private void loadScene(LoadSceneMsg msg)
    {
        if(msg.SceneBuildIndex != -1)
            SceneManager.LoadScene(msg.SceneBuildIndex);
        if (msg.SceneBuildName != null)
            SceneManager.LoadScene(msg.SceneBuildName);
        if (msg.OnSceneLoaded != null)
            onSceneLoaded = msg.OnSceneLoaded;
    }

    /// <summary>
    /// 场景加载完成之后才能调用(回调)
    /// </summary>
    private void SceneManager_sceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
    {
        if (onSceneLoaded != null)
        {
            onSceneLoaded();
            onSceneLoaded = null;
        }
    }
}
